Repository navigation
Keep subshell requests from reparenting main-shell thread output - #1609
Open
Mystery01092000 wants to merge 1 commit into
Open
Mystery01092000 wants to merge 1 commit into
Mystery01092000 wants to merge 1 commit into
Conversation
Threads started by a main-shell cell have no parent of their own, so their output falls back to the parent of the most recent shell request. That fallback (Kernel._shell_context and the global parent of the shell, display publisher, displayhook and output streams) was updated by set_parent on every thread, so a request handled on a subshell redirected later output of main-shell threads to the subshell request. On a subshell thread, set_parent now only sets the parent for that thread and leaves the fallback to the main shell. Fixes ipython#1603
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Output from a thread started by a main-shell cell was parented to whichever shell request the kernel handled most recently, including a request handled on a subshell. Any tool that uses subshells to stay responsive (variable inspectors and similar) could then take over the output of the main shell's background threads, and frontends dropped that output because they do not route that parent to the cell.
After this change, subshell requests no longer move that fallback. Threads with no parent of their own still go to the most recent main-shell request, as discussed in #1289.
Root cause
A plain
threading.Threadstarts with an empty contextvars context, so it never finds a parent set for its own thread and uses the shared fallback:Kernel._get_shell_context_varfalls back toKernel._shell_context.OutStreams fall back to their_parent_header_global.Kernel.set_parentreplaced_shell_contextwithcopy_context(), andIPythonKernel.set_parentcalledshell.set_parent(), which sets every global parent. Both ran on every request, on whichever thread handled it, so a request handled on a subshell thread overwrote the fallback used by the main shell's threads.Fix
When
set_parentruns on aSubshellThread:Kernel.set_parentstill sets the_shell_parent/_shell_parent_identcontext variables for that thread, but leaves_shell_contextalone.IPythonKernel.set_parentcallsshell.set_thread_parent(parent)(an existing API that sets only the calling thread's parent on the shell, displayhook, display publisher and streams) instead ofshell.set_parent(parent).The subshell's own output still goes to its request, because each subshell has its own thread. Nothing changes on the main shell thread. One side effect: a thread started by a subshell cell now also falls back to the most recent main-shell request, not to the most recent request on any shell.
Tests
tests/test_subshells.py::test_subshell_request_does_not_reparent_main_shell_thread_output. A main-shell cell starts a thread that waits on an event. A subshell request then sets the event and waits for the thread. The thread printskernel.get_parent()["header"]["msg_id"]. The test checks that the stream message is parented to the main-shell cell and that its text is the main-shell cell's msg_id. This covers both the stream fallback and theKernelfallback. Onmainthe output is parented to the subshell request and the test fails. With this change it passes.Fixes #1603
This change was prepared with help from an AI assistant. I reviewed it and ran the tests locally.